package com.up360.rapidcalc.box
{
	import com.up360.rapidcalc.global.LayerManager;
	import com.up360.rapidcalc.ui.BaseUI;
	
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	
	/**
	 * 作者:郎福涛
	 * 日期:2012-4-5 上午10:12:58
	 * 说明:弹出框基类,默认有至少有一个确定按钮
	 */
	public class BaseBox extends BaseUI
	{
		private var _onOK:Function;
		private var _onOKParams:Array;
		private var _isForbidHandle:Boolean;
		private var _mcDragBar:MovieClip;
		
		protected var _boxUI:MovieClip;
		//拖动范围
		private var _dragBound:Rectangle;
		
		/**
		 * 弹出框基类
		 * @param ui 弹出框ui
		 * @param text 文字内容
		 * @param isForbidHandle 是否禁用其它内容的鼠标操作
		 * @param onOK 确定按钮点击回调方法,有一个Object类型参数
		 * @param onOKParams 确定按钮点击回调方法函数
		 * @param textValignInCenter 文本框是否在上下对齐,上下对齐的位置相对于UI中绘制的文本框的范围 
		 */		
		public function BaseBox(ui:MovieClip,isForbidHandle:Boolean,onOK:Function,onOKParams:Array)
		{
			_onOK=onOK;
			_onOKParams=onOKParams;
			
			_boxUI=ui;
			addChild(_boxUI);
			
			_isForbidHandle=isForbidHandle;
			_mcDragBar=_boxUI.mcDragBar;
			
			LayerManager.boxLayer.addChild(this);
			_boxUI.btnOK.addEventListener(MouseEvent.CLICK,btnOKClick);
			if(_mcDragBar){
				_mcDragBar.buttonMode=true;
				_dragBound=new Rectangle(0,0,LayerManager.stage.stageWidth-this.width,LayerManager.stage.stageHeight-this.height);
				_mcDragBar.addEventListener(MouseEvent.MOUSE_DOWN,mcDragBarMouseDown);
				LayerManager.stage.addEventListener(MouseEvent.MOUSE_UP,stageMouseUp);
			}
			
			this.x=(LayerManager.stage.stageWidth-this.width)/2;
			this.y=(LayerManager.stage.stageHeight-this.height)/2;
		}
		
		private function mcDragBarMouseDown(e:MouseEvent):void
		{
			this.startDrag(false,_dragBound);
//			this.startDrag(false);
		}
		private function stageMouseUp(e:MouseEvent):void
		{
			this.stopDrag();
		}
		public function get isForbidHandle():Boolean{
			return _isForbidHandle;
		}
		private function btnOKClick(e:MouseEvent):void
		{
			if(_onOK!=null){
				_onOK.apply(this,_onOKParams);
			}
			BoxManager.closeBox(this);
		}
		
		override public function dispose():void{
			_boxUI.btnOK.removeEventListener(MouseEvent.CLICK,btnOKClick);
			_onOK=null;
			_onOKParams=null;
			if(_mcDragBar){
				_mcDragBar.removeEventListener(MouseEvent.MOUSE_DOWN,mcDragBarMouseDown);
				LayerManager.stage.removeEventListener(MouseEvent.MOUSE_DOWN,stageMouseUp);
				_mcDragBar=null;
			}
			super.dispose(); 
		}
		
	}
}